import { createRouter, createWebHashHistory, type Router as VRouter } from 'vue-router'
import { baseURL } from '@web/config'
import route from './route'

export class Router {
  /**
   * 保存实例
   *
   * @protected
   * @static
   * @type {(VRouter | null)}
   * @memberOf Router
   */
  protected static router: VRouter | null = null

  /**
   * 初始化
   *
   * @static
   * @param {string} [activeRule=baseURL]
   * @returns
   *
   * @memberOf Router
   */
  static create(activeRule = baseURL) {
    if (this.router) {
      return this.router
    }

    return (this.router = createRouter({
      routes: route.routes,
      history: createWebHashHistory(activeRule),
      scrollBehavior(to, from, savedPosition) {
        // 始终滚动到顶部
        return { top: 0 }
      },
    }))
  }

  /**
   * 销毁
   *
   * @static
   *
   * @memberOf Router
   */
  static destroy() {
    this.router?.clearRoutes()
    this.router = null
  }

  /**
   * 前往管理系统首页
   */
  static navigateToSystem() {}

  /**
   * 前往登录页
   */
  static navigateToLogin() {}
}
